//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Xml;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet.Pivot
{
    internal class SharedItems : IOfficeXmlSerializable
    {
        public PivotTableValue this [int i]
        {
            get
            {
                return _items[i];
            }
        }
        private const string XmlName = "sharedItems";
        readonly List<PivotTableValue> _items = new List<PivotTableValue>();
        private Boolean _containsBlank;
        private Boolean _containsDate;
        private Boolean _containsInteger;
        private Boolean _containsMixedTypes;
        private Boolean _containsNonDate;
        private Boolean _containsNumber;
        private Boolean _containsSemiMixedTypes;
        private Boolean _containsString;
        private UInt32? _count;
        private Boolean _longText;
        private DateTime? _maxDate;
        private double? _maxValue;
        private DateTime? _minDate;
        private double? _minValue;
        
        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(XmlName))
            {
                _containsSemiMixedTypes = c.GetOptionalBoolean("containsSemiMixedTypes", true);
                _containsNonDate = c.GetOptionalBoolean("containsNonDate", true);
                _containsDate = c.GetOptionalBoolean("containsDate", false);
                _containsString = c.GetOptionalBoolean("containsString", true);
                _containsBlank = c.GetOptionalBoolean("containsBlank", false);
                _containsMixedTypes = c.GetOptionalBoolean("containsMixedTypes", false);
                _containsNumber = c.GetOptionalBoolean("containsNumber", false);
                _containsInteger = c.GetOptionalBoolean("containsInteger", false);
                _minValue = c.GetOptionalDouble("minValue");
                _maxValue = c.GetOptionalDouble("maxValue");
                string ss = c.GetOptionalString("minDate");
                if (ss != null)
                    _minDate = DateTime.Parse(ss);
                else
                    _minDate = null;
                ss = c.GetOptionalString("maxDate");
                if (ss != null)
                    _maxDate = DateTime.Parse(ss);
                else
                    _maxDate = null;
                _count = c.GetOptionalUInteger("count");
                _longText = c.GetOptionalBoolean("longText", false);
                while (c.ReaderLocalName != null)
                    _items.Add(PivotTableValue.Read(c));
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(XmlName))
            {
                c.SetOptionalBoolean("containsSemiMixedTypes", _containsSemiMixedTypes, true);
                c.SetOptionalBoolean("containsNonDate", _containsNonDate, true);
                c.SetOptionalBoolean("containsDate", _containsDate, false);
                c.SetOptionalBoolean("containsString", _containsString, true);
                c.SetOptionalBoolean("containsBlank", _containsBlank, false);
                c.SetOptionalBoolean("containsMixedTypes", _containsMixedTypes, false);
                c.SetOptionalBoolean("containsNumber", _containsNumber, false);
                c.SetOptionalBoolean("containsInteger", _containsInteger, false);
                c.SetOptionalDouble("minValue", _minValue);
                c.SetOptionalDouble("maxValue", _maxValue);
                if (_minDate != null)
                    c.SetDate("minDate", (_minDate.Value));
                if (_maxDate != null)
                    c.SetDate("maxDate", _maxDate.Value);
                c.SetOptionalUInteger("count", _count);
                c.SetOptionalBoolean("longText", _longText, false);
                foreach (var item in _items)
                {
                    c.WriteElement(item);
                }
            }
        }

        #endregion
    }
}